package com.dwj.auto.comment.config.mybatis.plus;

import com.dwj.auto.comment.model.vo.StuClassVo;
import com.dwj.auto.comment.model.vo.UserRoleVo;
import com.dwj.auto.comment.model.vo.UserVo;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

@Intercepts({@Signature(
                type = Executor.class,
                method = "query",
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class InjectRolesInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //获取用户权限  并且 添加到sql参数中
        Object[] sqlParams = invocation.getArgs();
        Object sqlParam = sqlParams[1];
        if(sqlParam instanceof Map){
            Map<String, Object> param = (Map<String, Object>) sqlParam;
            for (Object paramObj : param.values()) {
                if(paramObj instanceof QueryCriteria){
                    QueryCriteria queryCriteria = (QueryCriteria) paramObj;
                    //开启注入角色 并且 roleIds为空才注入
                    if(queryCriteria.isInjectRole() && CollectionUtils.isEmpty(queryCriteria.getUserRoleIds())){
                        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                        if(principal != null){
                            UserVo userVo = (UserVo) principal;
                            List<UserRoleVo> userRole = userVo.getUserRole();
                            if(!CollectionUtils.isEmpty(userRole)){
                                queryCriteria.setUserRoleIds(userRole.stream().map(UserRoleVo::getId).collect(Collectors.toList()));
                            }
                        }
                    }
                    //开启注入班级参数 并且 classIds为空才注入
                    if(queryCriteria.isInjectClass() && CollectionUtils.isEmpty(queryCriteria.getUserClassIds())){
                        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                        if(principal != null){
                            UserVo userVo = (UserVo) principal;
                            List<StuClassVo> userClass = userVo.getUserClass();
                            if(!CollectionUtils.isEmpty(userClass)){
                                queryCriteria.setUserClassIds(userClass.stream().map(StuClassVo::getId).collect(Collectors.toList()));
                            }
                        }
                    }
                }
            }
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this); // 返回代理类
    }

    @Override
    public void setProperties(Properties properties) {

    }
}